function nano(template, data) {
	return template.replace(/\{([\w\.]*)\}/g, function(str, key) {
			var keys = key.split("."), v = keys.length == 0 ? undefined : data[keys.shift()];
			for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
			return (typeof v !== "undefined" && v !== null) ? v : "";
		});
}

/**
 * 使用函数的本地文本块
 */
function hereDoc(func) {
    return func.toString().split(/\n/).slice(1, -1).join('\n');
}

/**
 * 使用函数的本地文本块模板
 */
function hereTemplate(func, data){
  var s = hereDoc(func);
  s = nano(s, data);
  return s;
}

function show_table(u){
    $.ajax({url:u,
            dataType:"json",
            async:true,
            success : function(msg, textStatus) { 
                var s = "";
                $('#content').html('');
                var template = $("#template_plate").html();
                for(var i in msg){
                    var tmp =msg[i];
                    s = nano(template, tmp);
                    $('#content').append(s);
                }
            }
    },"text");

    //$('#content').html(s);
}

function show_tags(u){
    $.ajax({url:u,
            dataType:"json",
            async:true,
            success : function(msg, textStatus) { 
                var s = "";
                $('#tag_content').html('');
                var template = $("#template_tag").html();
                for(var i in msg){
                    var tmp =msg[i];
                    s = nano(template, tmp);
                    $('#tag_content').append(s);
                }
            }
    },"text");

    //$('#content').html(s);
}

function show_msg(pos, msg){
  $(pos).stop(true, true).show().text(msg).fadeTo(800).delay(2000).fadeOut(800);
}

function encodeHtml(s){
  return (typeof s != "string") ? s :
    s.replace(this.REGX_HTML_ENCODE,
        function($0){
          var c = $0.charCodeAt(0), r = ["&#"];
          c = (c == 0x20) ? 0xA0 : c;
          r.push(c); r.push(";");
          return r.join("");
        });
}

function decodeHtml(s){
  return (typeof s != "string") ? s :
    s.replace(this.REGX_HTML_DECODE,
        function($0,$1){
          var c = this.HTML_ENCODE[$0]; // 尝试查表
          if(c === undefined){
            // Maybe is Entity Number
            if(!isNaN($1)){
              c = String.fromCharCode(($1 == 160) ? 32 : $1);
            }else{
              // Not Entity Number
              c = $0;
            }
          }
          return c;
        });
}

/**
 * 载入语言
 */
function switch_lang(lang){
    var jsname = "./js/lang/"+ gLang+ ".js";
  $.getScript(jsname, function(){
    for(var i in gLangData){
      var k = i;
      var v = gLangData[k];
      $("." + k).text(v);
    }
  });
}

//加入收藏夹
function AddFavorite() {
    var title = document.title;
    var url = location.href;
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else {
        alert("由于浏览器的设置，添加收藏失败！\n请按【Ctrl+D】快捷键收藏本页。")
        return true;
    }
}


function getMarkdown(s){
  var eleID = "hide-result";
  var opts = {
    container: document.getElementById(eleID),
    textarea: null,

    button: {
      preview: true,
      fullscreen: true,
      bar: "none"
    },

    string: {
      togglePreview: '预览',
      toggleEdit: '编辑模式',
      toggleFullscreen: '全屏'
    },
    autogrow: true
  }
  $("#"+eleID).hide();
  var editor = new EpicEditor(opts).load();
  editor.importFile('some-file',s);
  editor.reflow('height');
  editor.preview();
  var content = editor.getElement('previewer').body.innerHTML;
  return content;
}

/**
 * 根据数据值情况获得默认值
 * @param  {string} s 数据
 * @param  {string} d 默认数据
 * @return {string}   结果
 */
function gdefault(s, d){
  if(typeof d === 'undefined') d = "";
  if(!s) return d;
  return s;
}

function getColorByGeneration(i){
  if(i < 0) i = -i;
  i = parseInt(i);

  var gGenColors=[
    "#c6c7e2",
    "#30d0c6",
    "#7c68fd",
    "#3498DB",
    "#16A085",
    "#E74C3C",
    "#feb563"
  ];
  i = i % gGenColors.length;
  return gGenColors[i];
}